WRANGLERS

Global Access to Water and Sanitation

Filter

Photo by Gallery DS on Unsplash

Photo by Gallery DS on Unsplash

Obviously, we all look at things through the filter of our own experiences….
— Malcolm-Jamal Warner


Ingest

df <- read.csv('./archetypes/access-to-water-and-sanitation/access-to-water-and-sanitation.csv', header = TRUE, stringsAsFactors = FALSE)
df

Wrangle

Where equal

Filter where rows equal to Africa in the Continent column.

df_1 <- df %>% filter(Continent=="Africa")
df_1

Where not equal

Taking only African countries into consideration, let’s say we want to eliminate, for some reason, Angola from Country from this data

df_2 <- df %>% filter((Continent=="Africa") & !(Country=="Angola"))
df_2

Where greater than

Filter dataset where Population is greater than 50,000.

df_6 <- df_2%>%  filter(Population > 50000)
df_6

Where less than

Filter dataset where Population is less than 50,000.

df_8 <- df_2 %>%  filter(Population < 50000)
df_8

Where between date

First, let’s convert the column Date into date format.

df_9 <- df
df_9$Date<-as.Date(df_9$Date, tryFormats = c("%Y-%m-%d"),optional = FALSE)
df_9

Filter dataset where Date is between 2010-2015:

df_10 <- df_9%>% filter(between(Date, as.Date("2010-01-01"), as.Date("2015-01-01")))
df_10

Where before date

Filter rows where Date is before 2000.

df_11 <- df_9 %>% filter(Date < as.Date("2000-01-01"))
df_11

Where after date

Filter rows where Date is after 2010 for the countries with No access to water for a population that is bigger than 75,000.

df_12 <- df_9%>% filter(Date > as.Date("2010-01-01") & (Metric=="No access to water") & (Population > 75000))
df_12

Based on regular expression (regex)

Filter the dataset where Continent name starts with S:

df_13 <- df %>% filter(str_detect(Continent, "^S"))
df_13

References

The citations and data sources used for this case

  • Narrative and Data Sources: Global Clean Water and Sanitation, Unicef
@misc{unicef_sdg,
  author = {UNICEF},
  title = {SDG Goal 6: Clean Water and Sanitation},
  url = {https://data.unicef.org/sdgs/goal-6-clean-water-sanitation/#:~:text=Goal%206%20aims%20to%20ensure%20availability%20and%20sustainable},
  urldate = {2021-03-18},
  organization = {UNICEF DATA}
}